{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating a Filter, Edge Detection"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import resources and display image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.image as mpimg\n",
    "\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "# Read in the image\n",
    "image = mpimg.imread('data/curved_lane.jpg')\n",
    "\n",
    "plt.imshow(image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert the image to grayscale"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Convert to grayscale for filtering\n",
    "gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n",
    "\n",
    "plt.imshow(gray, cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TODO: Create a custom kernel\n",
    "\n",
    "Below, you've been given one common type of edge detection filter: a Sobel operator.\n",
    "\n",
    "The Sobel filter is very commonly used in edge detection and in finding patterns in intensity in an image. Applying a Sobel filter to an image is a way of **taking (an approximation) of the derivative of the image** in the x or y direction, separately. The operators look as follows.\n",
    "\n",
    "<img src=\"notebook_ims/sobel_ops.png\" width=200 height=200>\n",
    "\n",
    "**It's up to you to create a Sobel x operator and apply it to the given image.**\n",
    "\n",
    "For a challenge, see if you can put the image through a series of filters: first one that blurs the image (takes an average of pixels), and then one that detects the edges."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a custom kernel\n",
    "\n",
    "# 3x3 array for edge detection\n",
    "sobel_y = np.array([[ -1, -2, -1], \n",
    "                   [ 0, 0, 0], \n",
    "                   [ 1, 2, 1]])\n",
    "\n",
    "## TODO: Create and apply a Sobel x operator\n",
    "\n",
    "\n",
    "# Filter the image using filter2D, which has inputs: (grayscale image, bit-depth, kernel)  \n",
    "filtered_image = cv2.filter2D(gray, -1, sobel_y)\n",
    "\n",
    "plt.imshow(filtered_image, cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test out other filters!\n",
    "\n",
    "You're encouraged to create other kinds of filters and apply them to see what happens! As an **optional exercise**, try the following:\n",
    "* Create a filter with decimal value weights.\n",
    "* Create a 5x5 filter\n",
    "* Apply your filters to the other images in the `images` directory.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
